package de.tud.stg.emergent.policy.utils;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;

import org.apache.neethi.Policy;

import de.tud.stg.emergent.policy.persistence.ClientEntity;
import de.tud.stg.emergent.policy.persistence.PolicyEntity;

public class DBUtils {

	
private static volatile DBUtils instance;
    

	

    public static DBUtils getInstance() {
    	DBUtils localInstance = instance;
    if (localInstance == null) {
        synchronized (DBUtils.class) {
            localInstance = instance;
            if (localInstance == null) {
                instance = localInstance = new DBUtils();
            }
        }
    }
    return localInstance;
} 
	
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}
	  public static Long addClient(Policy neethiPolicy) {
     	 
		// Open a database connection
			// (create a new database if it doesn't exist yet):
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("$objectdb/db/policy.odb");
		EntityManager em = emf.createEntityManager();


		  
		 ClientEntity client = new ClientEntity();  
		  
     	 PolicyEntity policyEntity = new PolicyEntity(neethiPolicy);
     	 
     	 client.setPolicy(policyEntity);
 
          em.getTransaction().begin();
 
          em.persist(policyEntity);
          em.persist(client);
 
          em.getTransaction().commit();
 
          // Close the database connection:
          em.close();
          emf.close();
			
          return client.getId();
      }    
	  
	  
	  public static void updateClient(long clientId, Policy neethiPolicy) {

		  // Open a database connection
          // (create a new database if it doesn't exist yet):
          EntityManagerFactory emf =
              Persistence.createEntityManagerFactory("$objectdb/db/policy.odb");
          EntityManager em = emf.createEntityManager();
     	 
  
          PolicyEntity updatePolicy = new PolicyEntity(neethiPolicy);
         
          ClientEntity updateClient = null;
          
          TypedQuery<ClientEntity> query =
              em.createQuery("SELECT c FROM PolicyClient c WHERE c.id = :clientId", ClientEntity.class);
          query.setParameter("clientId", clientId);

          List<ClientEntity> results = query.getResultList();
          for (ClientEntity c : results) {
              if(c.getId()==clientId){
            	  
             	 c.setPolicy(updatePolicy);
             	 
             	 updateClient = c;
             	 break;
              }
          }
           
          em.getTransaction().begin();
  
          em.persist(updateClient);
 
          em.getTransaction().commit();
          
          em.close();
          emf.close();
          
		   
	  }    
      
      public static Policy getClientPolicy(long clientId){
     	 
     	 PolicyEntity returnPolicy = null;
     	 
     	 // Open a database connection
          // (create a new database if it doesn't exist yet):
          EntityManagerFactory emf =
              Persistence.createEntityManagerFactory("$objectdb/db/policy.odb");
          EntityManager em = emf.createEntityManager();
     	 
          // Retrieve all the Point objects from the database:
          TypedQuery<ClientEntity> query =
              em.createQuery("SELECT c FROM ClientEntity c WHERE c.id = :clientId", ClientEntity.class);
          
          query.setParameter("clientId", clientId);
          
          List<ClientEntity> results = query.getResultList();
          for (ClientEntity p : results) {
              if(p.getId()==clientId)
              { 
             	 returnPolicy = p.getPolicy();
             	 break;}
          }
          em.close();
          emf.close();
          
          return CommonPolicyUtils.getNeethiFromStringPolicy(returnPolicy.getPolicyString());
          
      }
}
